<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/layui/layui.all.js"></script>

</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      评论列表
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th>作者</th>
            <th>评论</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody id="tbody">

        </tbody>
      </table>
      <div class="row text-center" id="test1">
      </div>
    </div>
  </div>
  <script src="./libs/art-template/template-web.js"></script>
  <script src="./js/http.js"></script>
  <script type="text/html" id="getList">
    {{each data}}
    {{if $value.state=='已拒绝'}}
         <tr class="danger">
    {{else}}
           <tr>
    {{/if}}
            <td>{{$value.author}}</td>
            <td>{{$value.content}}</td>
            <td>{{$value.title}}</td>
            <td>{{$value.date}}</td>
            <td>{{$value.state}}</td>
            <td class="text-center">
              {{if $value.state=='已拒绝'}}
              <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="retify({{$value.id}})">批准</a>
              {{else if $value.state=='已通过'}}
              <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="refuse({{$value.id}})">拒绝</a>
              {{else}}
               <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="retify({{$value.id}})">批准</a>
              <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="refuse({{$value.id}})">拒绝</a>
              {{/if}}
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs" onclick="del({{$value.id}})">删除</a>
            </td>
          </tr>
    {{/each}}
  </script>
  <script>
    $(function () {
      //获取所有数据渲染,两个参数page,perpage
      getList()
    })
    //获取所有评论数据
    let flag = true;
    function getList(page) {
      myHeader('/admin/comment/search', 'get', { page: page }, function (resData) {
        $('#tbody').html(template('getList', resData.data));
        // console.log(resData.dataresData);

        if (flag) {
          paging(resData.data.totalCount, page)
          flag = false;
        }
      })

    }

    //分页器
    function paging(total, page) {
      layui.use('laypage', function () {
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
          elem: 'test1', //注意，这里的 test1 是 ID，不用加 # 号
          limit: 6,    //每页条数
          count: total, //数据总数，从服务端得到

          jump: function (obj, first) {
            //obj包含了当前分页的所有参数，比如：
            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            // console.log(obj.limit); //得到每页显示的条数
            //首次不执行
            if (!first) {
              getList(obj.curr)
            }
          }
        });
      });
    }

    //批准
    function retify(r_id) {
      myHeader('/admin/comment/pass', 'post', { id: r_id }, function (resData) {
        if (resData.code !== 200) {
          layer.msg(resData.msg, { icon: 6 });
          return false;
        }
        layer.msg(resData.msg, { icon: 6 });
        getList()
      })
    }
    //拒绝
    function refuse(r_id) {
      layer.confirm('确定要拒绝掉此条申请吗?', { icon: 3, title: '提示' }, function (index) {
        //do something
        myHeader('/admin/comment/reject', 'post', { id: r_id }, function (resData) {
          if (resData.code !== 200) {
            layer.msg(resData.msg);
            return false;
          }
          layer.msg(resData.msg);
          getList()
        })
        layer.close(index);
      });
    }
    //删除
    function del(d_id) {
      layer.confirm('您确定需要删除此条评论?', { icon: 3, title: '提示' }, function (index) {
        //do something
        myHeader('/admin/comment/delete', 'post', { id: d_id }, function (resData) {
          if (resData.code != 200) {
            layer.msg(resData.msg);
            return false;
          }
          layer.msg(resData.msg)
          flag = true;
          getList()
        })
        layer.close(index);
      });
    }
  </script>
</body>

</html>